package edu.pku.sei.transformation.editor.controller.commands.node;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.transformation.editor.model.execution.AtomicTransformationExecution;
import edu.pku.sei.transformation.editor.model.execution.RuleExecution;

public class RuleExecutionCreateCommand extends Command {
	private RuleExecution ruleExe;
	private AtomicTransformationExecution transExe;
	private int index;

	public RuleExecutionCreateCommand(RuleExecution child,
			AtomicTransformationExecution father, int index) {
		this.ruleExe = child;
		this.transExe = father;
		this.index = index;
	}

	@Override
	public boolean canExecute() {
		if (ruleExe == null || transExe == null)
			return false;
		else
			return true;
	}

	@Override
	public void execute() {
		ruleExe.setContext(transExe);
		if (index < 0 || index > transExe.getSpecifiedRules().size())
			transExe.getSpecifiedRules().add(ruleExe);
		else
			transExe.getSpecifiedRules().add(index, ruleExe);
	}

	@Override
	public void undo() {
		ruleExe.setContext(null);
		transExe.getSpecifiedRules().remove(ruleExe);
	}

}
